#include "precompiled.h"
//#include "TTimer.h"
#include "grfTest.h"

//
void grfTest::TestTWindow()
{

/**************************************
  struct TTool : public grf::TWindow
  {
    grf::TImageGdiP Image;
  
    TTool(grf::TWindow *Parent) : grf::TWindow(Parent)
    {                        
      Image.LoadFromFile("d:\\YYY_orel.png");
    }
    virtual void TWindow_Paint(grf::TPaint &Paint)
    {
      Paint.DC.FillRect(Paint.DC.RClientPaint,grf::TBrush(grf::TColor(0,0,100)) );
      Image.TImage_Draw(Paint.DC,grf::TRect(0,0,Image.Width(),Image.Height()));
    }
    virtual void TWindow_Mouse(grf::TMouse &Mouse)
    {
      ::SetCursor(::LoadCursor(NULL,IDC_ARROW));
    }
    virtual void TWindow_MouseEnter() 
    {
    }
    virtual void TWindow_MouseExit() 
    {
    }
  };
  
  struct TBox : public TTool
  {
    TBox(grf::TWindow *Parent) : TTool(Parent)
    {
    }
   ~TBox()
    {
    }
    virtual void Show()
    {
      grf::TWindow::Show(); 
    }
    virtual void TWindow_Activate(bool Active) 
    {
      if (!Active && !MouseIsEntered()) { Hide();}
    }
    virtual void TWindow_Mouse(grf::TMouse &Mouse)
    {
      ::SetCursor(::LoadCursor(NULL,IDC_CROSS));
    }
  };
  
  struct TMenu : public grf::TWindow
  {
    struct TType : public grf::TWindow::TType { virtual TType &Type() { static TType T; return T;}  };
    virtual TType &Type() { return TType().Type();}
    
    grf::TImageGdiP Image;

    TMenu(grf::TWindow *Parent) : grf::TWindow(Parent)
    {
      Image.LoadFromFile("d:\\tree.bmp");
    }
    void Popup(grf::TPoint PtClient)
    {
      grf::TSize Size = Image.Size(); if (!Size.Width) { Size = grf::TSize(100,100);}
      MoveWindow(grf::TRect(Parent()->ClientToScreen(PtClient),Size),true); 
      SetActive(); Show();
    }
    virtual void TWindow_Paint(grf::TPaint &Paint)
    {
      Paint.DC.FillRect(Paint.DC.RClientPaint,grf::TBrush(grf::TColor(0,0,100)));
      Image.TImage_Draw(Paint.DC,grf::TRect(0,0,Image.Width(),Image.Height()));
    }
    virtual void TWindow_Mouse(grf::TMouse &Mouse)
    {
      ::SetCursor(::LoadCursor(NULL,IDC_ARROW)); switch (Mouse.State)
      {
        case grf::TMouse::EState_LButtonDown:
        {
          grf::TPoint Pm = Mouse.PtClient;
          
   //Children_Delete(); 
   //Pm.X = GetClientRect().Right();
        
          TMenuChild *Child = new TMenuChild(this);
          Child->Popup(Pm);
        }
      }
    }
    virtual void TWindow_Close() { grf::TWindow::TWindow_Close(); Children_Delete();}
  };

  struct TMenuChild : public TMenu
  {
    struct TType : public TMenu::TType { virtual TType &Type() { static TType T; return T;} };
    virtual TType &Type() { return TType().Type();}
    
    TMenuChild(grf::TWindow *Parent) : TMenu(Parent) 
    {
      //SetWindowLong(GWL_STYLE,WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX);
      SetWindowLong(Handle(),GWL_STYLE,WS_POPUP|WS_CAPTION);
      SetEnDelete(true);
      Image.LoadFromFile("d:\\back35.bmp");

      //grf::TWindow *This = this;
      //grf::TWindow *P1 = SearchThisOrParent(grf::TWindow::TType());
      //grf::TWindow *P2 = dynamic_cast<TMenu*>(SearchThisOrParent(TMenu::TType()));
    }
    virtual void TWindow_Mouse(grf::TMouse &Mouse)
    {
      TMenu::TWindow_Mouse(Mouse); switch (Mouse.State)
      {
        case grf::TMouse::EState_RButtonDown:
        {
          struct TDelete : public svl::TPostMessage
          {
            TMenuChild *p; TDelete(TMenuChild *P) { p = P; Post();}
            virtual void TPostMessage_Receive()   { p->Hide(); p->Parent()->SetActive(); delete p;}
          };
          new TDelete(this);
        }
      }
    }
  };
*****************************/

/********************
  struct TWindowApp : public grf::TWindow
  {
    grf::TImageGdiP Image;
    TTool           Win,WinModal;
    TMenu           Menu;
    TBox            Box;

    TWindowApp() : Win(this),WinModal(this),Box(this),Menu(this)
    {
      Image.LoadFromFile("d:\\YYY_orel.png");
    }
    virtual void TWindow_Paint(grf::TPaint &Paint)
    {
      Paint.DC.FillRect(Paint.DC.RClientPaint,grf::TBrush(grf::TColor(0,0,100)) );
      Image.TImage_Draw(Paint.DC,grf::TRect(0,0,Image.Width(),Image.Height()));
    }
    virtual void TWindow_Mouse(grf::TMouse &Mouse)
    {
      ::SetCursor(::LoadCursor(NULL,IDC_ARROW)); switch (Mouse.State)
      {
        case grf::TMouse::EState_RButtonDown:
        {
          grf::TRect R(ClientToScreen(Mouse.PtClient),grf::TSize(100,300));

          if (Mouse.Shift.Contains(grf::TShift::Ctrl))
          { 
            struct TTimer : public Igor::TTimer
            {
              TTimer() : Igor::TTimer(0,1000) {}
              virtual void TTimer_Tick() { ::MessageBeep(0);}
            }
            Timer;

            WinModal.MoveWindow(R,true); WinModal.ShowModal();
          }
          else if (Mouse.Shift.Contains(grf::TShift::Shift))
          { 
            Menu.Popup(Mouse.PtClient);
          }
          else 
          { 
            //Win.MoveWindow(R,true); Win.Show();

            //DbgBreak("ssssss");
            //Except("wwwwwwwwwwwwwwww");
  //::MessageBox(::GetDesktopWindow(),"text","EXCEPTION",MB_ICONINFORMATION|MB_SYSTEMMODAL);

            if (!Box.IsActive())
            {
              Box.MoveWindow(R,true); Box.Show();
            }
          } 
        }
        break;

        case grf::TMouse::EState_LButtonDown:
        {
        }
        break;

        case grf::TMouse::EState_LButtonUp:
        {
        }
        break;
      }
    }
    private: svl_Except;
  };

  TWindowApp WindowApp; 
    WindowApp.MoveWindow(grf::TRect(10,10,200,400),true); 
    WindowApp.Show(); 
  WindowApp.MessagesLoop();
****************************/
  
}

////////////////////////////////////////////////////////////////////////////////

//
void grfTest::Test()
{
  TestTWindow();
  //TestTTimer();
}
